package com.example.common;

import org.apache.commons.lang3.StringUtils;

/**
 * @author  guochao
 * @date  2022/1/6 20:56
 * @version 1.0
 */
public class MySQLTimeFormat {
    //年
    public static final String YEAR_FORMAT = "%Y";
    //月
    public static final String MONTH_FORMAT = "%Y-%m";
    //日
    public static final String DAY_FORMAT = "%Y-%m-%d";

    //根据时间获取格式
    public static String getFormat(String time) {
        if (StringUtils.isNotBlank(time)) {
            try {
                String[] split = time.split("-");
                switch (split.length) {
                    case 3:
                        return DAY_FORMAT;
                    case 2:
                        return MONTH_FORMAT;
                    case 1:
                        return YEAR_FORMAT;
                }
            } catch (Exception e) {
            }
        }
        return DAY_FORMAT;
    }
}
